<?php


namespace App\Http\Controllers;


use App\Models\FishingSite;
use App\Models\LiveRoom;
use App\Models\User;
use Illuminate\Http\Request;

class LiveController extends Controller
{
    /**
     * 我的直播
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function myRoom(Request $request)
    {
        $user = User::where('id', $request->uid)->first();
        $result = [
            'now_live' => 0,
            'room' => [
                'avatar' => img_url($user->avatar),
                'name' => $user->name,
                'site_id' => 0,
                'link_keyword' => '',
                'status' => 0
            ]
        ];
        $room = LiveRoom::where('uid', $request->uid)->where('status', 1)->first();
        if (!$room) {
            return $this->success($result);
        }
        $name = $user->name;
        if ($room->site_id) {
            $name = $user->name . '【' . FishingSite::where('id', $room->site_id)->value('name') . '】';
        }
        $result['room']['name'] = $name;
        $result['room']['site_id'] = $room->site_id;
        $result['room']['link_keyword'] = $room->link_keyword;
        $result['room']['status'] = $room->status;
        $result['now_live'] = 1;
        return $this->success($result);
    }

    /**
     * 关闭直播
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function closeRoom(Request $request)
    {
        $room = LiveRoom::where('uid', $request->uid)->where('status', 1)->first();
        if ($room) {
            $room->status = 2;
            $room->save();
        }
        return $this->success();
    }

    /**
     * 提交直播间分享链接
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Throwable
     */
    public function createRoom(Request $request)
    {
        $isAnchor = User::where('id', $request->uid)->value('is_anchor');
        throw_err(!$isAnchor, '您还没有主播席位，请向客服申请。');
        throw_err(!$request->link_keyword, '请填写直播间分享链接');
        $liveing = LiveRoom::where(['uid' => $request->uid, 'status' => 1])->first();
        throw_err($liveing, '您正在直播中，请勿重复提交');
        $room = new LiveRoom();
        $room->uid = $request->uid;
        $room->name = $request->name;
        $room->link_keyword = $request->link_keyword;
        $room->status = 1;
        $room->site_id = $request->site_id ?? 0;
        $room->save();
        return $this->success();
    }

    /**
     * 直接间列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function room(Request $request)
    {
        $model = new LiveRoom();
        if ($request->site_id) {
            $model = $model->where('site_id', $request->site_id);
        }
        $data = $model->where('status', 1)->orderByDesc('id')->paginate(10);
        foreach ($data as &$item) {
            $user = User::where('id', $item->uid)->first();
            $item->avatar = img_url($user->avatar);

            //房间名
            $name = $user->name;
            if ($item->site_id) {
                $name = $user->name . '【' . FishingSite::where('id', $item->site_id)->value('name') . '】';
            }
            $item->name = $name;
            $item->img = self::backgroundImg();
        }
        return $this->success($data);
    }

    /**
     * 全部直播间
     * @return \Illuminate\Http\JsonResponse
     */
    public function allRoom()
    {
        $data = LiveRoom::where('status', 1)->orderByDesc('id')->paginate(10);
        foreach ($data as &$item) {
            $user = User::where('id', $item->uid)->first();
            $item->avatar = img_url($user->avatar);

            //房间名
            $name = $user->name;
            if ($item->site_id) {
                $name = $user->name . '【' . FishingSite::where('id', $item->site_id)->value('name') . '】';
            }
            $item->name = $name;
            $item->img = self::backgroundImg();
        }
        return $this->success($data);
    }

    /**
     * 直播间背景图
     * @return string
     */
    private static function backgroundImg()
    {
        return 'https://gz-fishing.oss-cn-heyuan.aliyuncs.com/static/WX20220526-224122.png';
    }

}